package com.chess.chessplay.controller;

import ch.qos.logback.core.pattern.color.ANSIConstants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chess.chessplay.pojo.Eval;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.nio.cs.ext.GBK;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.spi.CurrencyNameProvider;

@Controller
@RequestMapping("/compile")
public class CompileController {


    @ResponseBody
    @RequestMapping(value = "/getanswer", method = RequestMethod.POST)
    public String getAnswer(@RequestBody String code){


        code= code.replace("\\n", "\r\n");
        code = code.replace("\\","");
//        code = code.replaceAll("(?!\\r)\\n", "\r\n");
//
        //得到框架代码
        String admin_file_path = "E:\\java\\Test";
        String admin_file_name = "AdminCode.txt";
        String adcode = readFileContent(admin_file_path+"\\"+admin_file_name);

        //将用户代码拼接进去
        int size = adcode.length()-1;
        while(adcode.charAt(size) != '}'){
            size--;
        }
        adcode = adcode.substring(0, size);
        adcode += code.substring(1, code.length()-1);
        adcode += "\n}";

        //运行的java文件
        String cmpl_file_path = "E:\\java\\Test";
        String cmpl_file_name = "computer.java";

        writeFileContent(cmpl_file_path+"\\"+cmpl_file_name , adcode);

        Eval eval = new Eval(cmpl_file_path+"\\",cmpl_file_name);
        eval.compiler();
        eval.run();

        //输出答案的文件
        String ans_file_path = "E:\\java\\Test";
        String ans_file_name = "answer.txt";


        String answer = readFileContent(ans_file_path+"\\"+ans_file_name);
        Map<String, Object> map = new HashMap<>();
        map.put("answer", answer);
        String json = JSONObject.toJSONStringWithDateFormat(map,JSON.DEFFAULT_DATE_FORMAT);
//        String json = JSONObject.toJSONStringWithDateFormat(answer,JSON.DEFFAULT_DATE_FORMAT);这种方式传过去，前端无法接收到
        return json;
    }

    //将字符串写入指定目录文件
    public static void writeFileContent(String fileName, String content){

        try {
            File file = new File(fileName);
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
//            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,"GBK"));
            bw.write(content);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //读取文件中的内容
    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
}
